import java.util.*;

public class Solution924 {

    static class BingChaJi{
        int[] b;
        int[] size;
        int res;

        public BingChaJi(int n){
            b=new int[n];
            size=new int[n];
            for (int i=0;i<n;i++){
                b[i]=i;
                size[i]=1;
            }
            res=n;
        }

        public int find(int t){
            return b[t]==t?t:(b[t]=find(b[t]));
        }

        public void union(int x,int y){
            int p=find(x);
            int q=find(y);
            if (p!=q){
                b[p]=q;
                size[q]+=size[p];
            }
            res--;
        }
    }

    public int minMalwareSpread(int[][] graph, int[] initial) {
        BingChaJi bingChaJi=new BingChaJi(graph.length);
        for (int i=0;i<graph.length;i++){
            for (int j=i+1;j<graph.length;j++){
                if (graph[i][j]==1){
                    bingChaJi.union(i,j);
                }
            }
        }
        Arrays.sort(initial);
        Map<Integer, List<Integer>> map=new HashMap<>();
        for (int i=0;i<initial.length;i++){
            map.putIfAbsent(bingChaJi.find(initial[i]),new ArrayList<>());
            map.get(bingChaJi.find(initial[i])).add(i);
        }
        int min=Integer.MAX_VALUE;
        for (Map.Entry<Integer, List<Integer>> integerListEntry : map.entrySet()) {
            min=Math.min(min,integerListEntry.getValue().size());
        }
        if (min!=1){
            return initial[0];
        }
        int max=0;
        int ans=0;
        for (int i : initial) {
            if (map.get(bingChaJi.find(i)).size()==1 && bingChaJi.size[bingChaJi.find(i)]>max){
                max=bingChaJi.size[bingChaJi.find(i)];
                ans=i;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution924().minMalwareSpread(new int[][]{{1,1,0},{1,1,0},{0,0,1}},new int[]{0,1}));
    }
}
